<?php
class PushModel extends Model{
	
	public function add_devicetoken($data){
		M('uri','','DB_MEETING')->where('devicetoken="'.$data['devicetoken'].'" and app_for="'.$data['app_for'].'"')->delete();
		M('uri','','DB_MEETING')->where('uid='.$data['uid'].' and app_for="'.$data['app_for'].'"')->delete();	
		return M('uri','','DB_MEETING')->add($data);
	}
		
	public function get_data_by_devicetoken($data){
		return M('uri','','DB_MEETING')->where('devicetoken="'.$data['devicetoken'].'" and app_for="'.$data['app_for'].'"')->find();
	}
	
	public function get_data_by_uid($uid,$appfor){
		return M('uri','','DB_MEETING')->where('uid='.$uid.' and app_for="'.$appfor.'"')->find();	
	}
	
	//修改uid
	public function update_uid_by_devicetoken($data){
		$sql='update uri set uid='.$data['uid'].' where devicetoken="'.$data['devicetoken'].'" and app_for="'.$data['app_for'].'"';
		return M('uri','','DB_MEETING')->execute($sql);
	}
	
	//修改uid
	public function update_devicetoken_by_uid($data){
		$sql='update uri set devicetoken="'.$data['devicetoken'].'" where uid="'.$data['uid'].'" and app_for="'.$data['app_for'].'"';
		return M('uri','','DB_MEETING')->execute($sql);
	}

    //修改
    public function update_badge_number($badge,$data){
        return M('uri','','DB_MEETING')->execute("update uri set badge=".$badge." where uid={$data['uid']} and app_for='{$data['app_for']}'");
    }
// +---------------------------------------+
// | 修改当前推送小红点                    	   |
// +---------------------------------------+
// | last update author: zhanghao		   |
// | last update time  : 2014-11-05 16:55  |
// +---------------------------------------+
// | use for: Push/update_push_badge	   |
// +---------------------------------------+
	public function init_badge($data){
		return M('uri','','DB_MEETING')->where('devicetoken="'.$data['devicetoken'].'" and app_for="'.$data['app_for'].'" and uid='.$data['uid'])->save($data);	
	}

	public function get_meeting_uri($meeting_id){
		$audience = D('Meeting')->get_audience_by_meeting_id($meeting_id);
		$audiences= '';
		foreach($audience as $val){
			if(!$audience)
				$audience.=$val['audience_id'];
			else
				$audiences.=','.$val['audience_id'];
		}
		return M('uri','','DB_MEETING')->where('uid in ('.$audiences.') and app_for="meeting"')->find();
	}
	
}
?>